<template>
  <div
    v-loading="loading"
    v-empty="nopermission"
    class="rc-cont"
    xs-empty-icon="nopermission"
    xs-empty-text="暂无权限"
  >
    <flexbox v-if="!isSeas" class="rc-head" justify="space-between" align="end">
      <span class="color-02 size-14 font-bold">签署列表</span>
      <el-button
        v-if="
          saveRefundAuth &&
            (crmType == 'vipStudents' ||
              crmType == 'refund' ||
              crmType == 'customer') &&
            isAgreement
        "
        class="xr-btn--orange rc-head-item"
        icon="el-icon-plus"
        type="primary"
        @click="createClick"
        style="font-size: 14px"
        >发起签署</el-button
      >
    </flexbox>
    <el-table
      :data="list"
      height="400px"
      :cell-class-name="cellClassName"
      stripe
      style="width: 100%; border: 1px solid #e6e6e6"
      @row-click="handleRowClick"
    >
      <el-table-column
        v-for="(item, index) in fieldList"
        :key="index"
        :prop="item.prop"
        :formatter="fieldFormatter"
        :label="item.label"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <template v-if="item.prop == 'fun'">
            <span class="can-visit--underline" @click="lockRow(scope.row)"
              >预览协议</span
            >
            <span class="can-visit--underline" @click="copyLink(scope.row)"
              >复制签署链接</span
            >
          </template>
          <template v-else>
            {{ fieldFormatter(scope.row, scope.column) }}
          </template>
        </template>
      </el-table-column>
    </el-table>
    <c-r-m-full-screen-detail
      :visible.sync="showFullDetail"
      :id="contractId"
      crm-type="refund"
      @handle="detailHandle"
    />

    <sign-create
      v-if="isCreate"
      :show="isCreate"
      :action="createActionInfo"
      @save-success="hideView"
      @close="hideView"
    ></sign-create>
  </div>
</template>

<script type="text/javascript">
import TableMixin from "../mixins/Table";
import { mapGetters } from "vuex";
import CheckStatusMixin from "@/mixins/CheckStatusMixin";
import { separator } from "@/filters/vueNumeralFilter/filters";
import signCreate from "../vipStudents/signCreate.vue";

import {
  getStuAgreement,
  getCheckContract,
  getContractNum,
} from "@/api/admin/agreement";
import { debounce } from "throttle-debounce";
import { copyText } from "@/utils/index";
export default {
  name: "RelativeRefund", // 相关联系人  可能再很多地方展示 放到学员管理目录下
  components: {
    CRMFullScreenDetail: () => import("@/components/CRMFullScreenDetail"),
    signCreate
  },
  mixins: [CheckStatusMixin, TableMixin],
  props: {
    /** 模块ID */
    id: [String, Number],
    /** 没有值就是全部类型 有值就是当个类型 */
    crmType: {
      type: String,
      default: ""
    },
    /** 是公海 默认是学员 */
    isSeas: {
      type: Boolean,
      default: false
    },
    /** 联系人人下 新建商机 需要联系人里的学员信息  合同下需要学员和商机信息 */
    detail: {
      type: Object,
      default: () => {
        return {};
      }
    },
    isAgreement: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      loading: false,
      nopermission: false,
      list: [],
      fieldList: [],
      showFullDetail: false,
      isCreate: false, // 控制新建
      contractId: "", // 查看全屏联系人详情的 ID
      // 创建的相关信息
      createActionInfo: { type: "relative", crmType: this.crmType, data: {} }
    };
  },
  inject: ["rootTabs"],
  computed: {
    ...mapGetters(["crm", "agreement"]),
    saveRefundAuth() {
      if (this.agreement && this.agreement.sign && this.agreement.sign.save) {
        return true;
      }
      return false;
    }
  },
  watch: {
    id(val) {
      this.list = [];
      this.getDetail();
    },

    // 监听tab标签切换
    "rootTabs.currentName"(val) {
      if (val === "RelativeRefund") {
        this.getDetail(false);
      }
    }
  },
  mounted() {
    this.getDetail();
  },
  methods: {
    getFieldList() {
      this.fieldList.push({
        prop: "templateName",
        width: "200",
        label: "协议名称"
      });
      this.fieldList.push({
        prop: "createUserName",
        width: "200",
        label: "发起人"
      });
      this.fieldList.push({
        prop: "createTime",
        width: "200",
        label: "发起时间"
      });
      this.fieldList.push({
        prop: "signTime",
        width: "200",
        label: "签署时间"
      });
      this.fieldList.push({
        prop: "fun",
        width: "200",
        label: "操作"
      });
    },

    getDetail(loading = true) {
      this.loading = loading;
      getStuAgreement({
        customerId: this.detail.studentId || this.detail.customerId,
        customerType: this.crmType == "customer" ? "学员" : "vip学员",
        page: 1,
        limit: 999
      })
        .then(res => {
          if (this.fieldList.length == 0) {
            this.getFieldList();
          }
          this.nopermission = false;
          this.loading = false;
          this.list = res.data.list;
        })
        .catch(data => {
          if (data.code == 102) {
            this.nopermission = true;
          }
          this.loading = false;
        });
    },

    /**
     * 格式化字段
     */
    fieldFormatter(row, column) {
      if (this.moneyFields.includes(column.property)) {
        return separator(row[column.property] || 0);
      }
      return row[column.property] === "" || row[column.property] === null
        ? "--"
        : row[column.property];
    },

    /**
     * 当某一行被点击时会触发该事件
     */
    handleRowClick(row, column, event) {
      if (column.property == "refundNum") {
        this.contractId = row.refundId;
        this.showFullDetail = true;
      }
    },

    /**
     * 通过回调控制class
     */
    cellClassName({ row, column, rowIndex, columnIndex }) {
      if (column.property === "refundNum") {
        return "can-visit--underline";
      } else {
        return "";
      }
    },

    /**
     * 新建签署
     */
    createClick() {
      this.$emit("sign", this.detail);
      // this.createActionInfo.data = this.detail;
      // this.isCreate = true;
    },
    createSaveSuccess() {
      this.$bus.emit("crm-tab-num-update");
      this.getDetail();
    },

    /**
     * 详情操作
     */
    detailHandle(data) {
      if (data.type === "delete") {
        this.getDetail();
        this.$nextTick(() => {
          this.$bus.emit("crm-tab-num-update");
        });
      }
    },

    // 预览
    lockRow: debounce(500, function(row) {
      getCheckContract({
        contractNo: row.contractNo
      }).then(res => {
        if (res && res.code === 0) {
          window.open(res.data.url, "_blank");
        } else {
          this.$message.error(res.msg);
        }
      });
    }),
    // 复制签署链接
    copyLink: debounce(500, function(row) {
      getCheckContract({
        contractNo: row.contractNo
      }).then(res => {
        if (res && res.code === 0) {
          copyText.call(this, res.data.url);
        }
      });
    }),
    hideView() {
      console.log(123123);
      this.isCreate = false;
    }
  }
};
</script>
<style lang="scss" scoped>
@import "../styles/relativecrm.scss";
</style>
